package com.mybatisflex.gencode;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.config.TableDefConfig;
import com.zaxxer.hikari.HikariDataSource;

public class GenCodeApp {

//    private static final String Url = "jdbc:mysql://106.52.210.196:3306/vipboot?" +
//            "useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&serverTimezone=Asia/Shanghai";
//    private static final String User = "root";
//    private static final String Password = "Root3306!";

    private static final String Url = "jdbc:mysql://124.71.99.94:3306/vshop_saas?" +
            "useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&serverTimezone=Asia/Shanghai";
    private static final String User = "vshop_saas";
    private static final String Password = "vshop_saas123456";

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(Url);
        dataSource.setUsername(User);
        dataSource.setPassword(Password);

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        //GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
//        globalConfig.setAuthor("lizeda");
        globalConfig.setAuthor("vshop");


        //设置根包
        globalConfig.setBasePackage("com.vshop.platform.system");

        //设置表前缀和只生成哪些表
//        globalConfig.setTablePrefix("vip_");
        globalConfig.setTablePrefix("vshop_");
//        globalConfig.setGenerateTable("tb_account", "tb_account_session");
        globalConfig.setUnGenerateTable("vip_*");
//
        //设置生成 entity 并启用 Lombok
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        //设置项目的JDK版本，项目的JDK为14及以上时建议设置该项，小于14则可以不设置
        globalConfig.setEntityJdkVersion(17);
//        globalConfig.setEntity
//        globalConfig.setEntityClassSuffix("Base");
        globalConfig.setEntityPackage(globalConfig.getBasePackage() + ".domain.entity");

        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        globalConfig.setMapperAnnotation(true);
        globalConfig.setMapperPackage(globalConfig.getBasePackage() + ".crud.mapper");
        globalConfig.setMapperXmlGenerateEnable(true);
//        globalConfig.setMapperXmlPath(globalConfig.getBasePackage() + ".dal.mapper.xml");
//        globalConfig.setMapperXmlPath("src/main/java/" + globalConfig.getBasePackage() + ".dal.mapper.xml");
        globalConfig.setMapperXmlPath("src/main/java/" + "mapper/system");


        globalConfig.setServiceImplGenerateEnable(true);
//        globalConfig.setServiceImplClassSuffix("Service");
        globalConfig.setServiceImplClassSuffix("Dao");
//        globalConfig.setServiceImplPackage(globalConfig.getBasePackage() + ".serviceImpl");
//        globalConfig.setServiceImplPackage(globalConfig.getBasePackage() + ".service");
        globalConfig.setServiceImplPackage(globalConfig.getBasePackage() + ".crud.dao");
//        globalConfig.setServiceImplSuperClass("com.mybatisflex.spring.service.impl.ServiceImpl".getClass());

        globalConfig.setControllerGenerateEnable(true);

//        globalConfig.setTableDefGenerateEnable(true);
//        globalConfig.setTableDefPackage(globalConfig.getBasePackage() + ".dal.entity.table");
//        globalConfig.setTableDefInstanceSuffix("Table");
//        globalConfig.setTableDefPropertiesNameStyle(TableDefConfig.NameStyle.UPPER_CAMEL_CASE);

        //可以单独配置某个列
        ColumnConfig columnConfig = new ColumnConfig();
//        columnConfig.setColumnName("tenant_id");
        columnConfig.setLarge(true);
        columnConfig.setVersion(true);
//        globalConfig.setColumnConfig("tb_account", columnConfig);
//        columnConfig.setTypeHandler()

//        JdbcTypeMapping.registerMapping(Byte.class, String.class);

        return globalConfig;
    }
}
